package com.example.universitylibraryback.service;

import com.example.universitylibraryback.entity.Book;
import com.example.universitylibraryback.entity.BorrowRequest;
import com.example.universitylibraryback.entity.OverdueRecord;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RuleEngineService {
    
    @Autowired
    private KieContainer kieContainer;
    
    /**
     * 执行借阅规则验证
     */
    public BorrowRequest executeBorrowRules(BorrowRequest borrowRequest) {
        KieSession kieSession = kieContainer.newKieSession("BorrowRulesSession");
        try {
            kieSession.insert(borrowRequest);
            kieSession.fireAllRules();
            return borrowRequest;
        } finally {
            kieSession.dispose();
        }
    }
    
    /**
     * 执行罚款计算规则
     */
    public OverdueRecord executeFineCalculationRules(OverdueRecord overdueRecord) {
        KieSession kieSession = kieContainer.newKieSession("FineRulesSession");
        try {
            kieSession.insert(overdueRecord);
            kieSession.fireAllRules();
            return overdueRecord;
        } finally {
            kieSession.dispose();
        }
    }
    
    /**
     * 执行书籍状态管理规则
     */
    public Book executeBookStatusRules(Book book) {
        KieSession kieSession = kieContainer.newKieSession("BookStatusSession");
        try {
            kieSession.insert(book);
            kieSession.fireAllRules();
            return book;
        } finally {
            kieSession.dispose();
        }
    }
}